package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/11/22 9:41
 * @description：https://leetcode-cn.com/problems/shuffle-an-array/
 */
public class 打乱数组 {
    @Test
    public void test() {
        int[] nums = {-6, 10, 184};
        Solution solution = new Solution(nums);
        System.out.println(Arrays.toString(solution.shuffle()));
        System.out.println(Arrays.toString(solution.reset()));
        System.out.println(Arrays.toString(solution.shuffle()));
    }


    class Solution {

        private int[] sourceNums;
        private int n;
        private Random random = new Random();

        public Solution(int[] nums) {
            this.n = nums.length;
            this.sourceNums = nums;
        }

        public int[] reset() {
            return sourceNums;
        }

        // 洗牌算法
        public int[] shuffle() {
            int[] newNums = sourceNums.clone();
            for (int i = 0; i < n; i++) {
                charge(newNums, i, i + random.nextInt(n - i));
            }
            return newNums;
        }

        public void charge(int[] nums, int i, int j) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
    }

    /**
     * Your Solution object will be instantiated and called as such:
     * Solution obj = new Solution(nums);
     * int[] param_1 = obj.reset();
     * int[] param_2 = obj.shuffle();
     */
}
